﻿using com.guojin.mj.net.message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.guojin.core.io.message;
using UnityEngine;
using com.guojin.mj.net.message.game;
using UnityEngine.UI;
using DG.Tweening; 

    class ChatReciveMessagePage : Observer{
        private static ChatReciveMessagePage instance;
        public static ChatReciveMessagePage GetInstance()
        {
            return instance;
        }
        void Awake()
        {
            instance = this;
        }

        public void OnReceiveMsg1225(string msg, Message data) {
            PlayerReceiveChatInfo message = data as PlayerReceiveChatInfo;
            PlayerReceiveChatInfo resp = PlayerReceiveChatInfo.create(message.UserIndex, message.ReceiveChatInfo, message.Num);
            switch (resp.Num) {
                case 1:
                    //处理文本
                    SetTextChat(resp.UserIndex, resp.ReceiveChatInfo);
                    break;
                case 2:
                //处理表情
                PlayAni(resp.UserIndex, resp.ReceiveChatInfo);
                    break;
                case 3:
                    //处理语音
                    SetUsefulWord(resp.UserIndex,resp.ReceiveChatInfo);
                    break;
            }
            Debug.Log(data.toString());
        }

        private void SetTextChat(int index, string textchat)
        {
            Transform target = transform.FindChild(string.Format("loadPointGroup/player{0}/headPoint/playerHead", GameController.GetInstance().intArr[index]));
            GameObject chatText = TooL.LoadPrefab(target.gameObject, "ChatText");
            chatText.transform.FindChild("chatText").GetComponent<Text>().text = textchat;

            Destroy(chatText, 3.0f);
        }
       
        //private void SetEmoji(int index, string emojiID) {

        //    Transform target = transform.FindChild(string.Format("loadPointGroup/player{0}/headPoint/playerHead", GameController.GetInstance().intArr[index]));
        //    GameObject emoji = new GameObject();
        //    emoji.name = "emoji";
        //    emoji.AddComponent<Image>().sprite = Resources.Load<Sprite>("Emoji/" + emojiID);

        //    emoji.transform.SetParent(target);
        //    emoji.transform.localScale = Vector3.one * 1.5f;

        //    if (target.localPosition.x > 0) {
        //        emoji.transform.localPosition = Vector3.zero + new Vector3(-25, -25, 0);
        //    } else {
        //        emoji.transform.localPosition = Vector3.zero + new Vector3(25, 25, 0);
        //    }

        //    emoji.GetComponent<Image>().color = new Color(1, 1, 1, 0);
        //    emoji.GetComponent<Image>().DOFade(1, 0.5f);

        //    Destroy(emoji, 1.5f);
        //}

    private void PlayAni( int a , string name)
    {
        Debug.Log(name);
        var clone = Instantiate(Resources.Load("emojipre/" + name) as GameObject);
        GameObject parentNode = GameObject.Find("AniCamera");
        clone.transform.SetParent(parentNode.transform.GetChild( GameController.GetInstance().intArr[a]), false);
        clone.transform.localPosition = new Vector3(0, 0, -5);
    }


    private void SetUsefulWord(int index, string wordID) {
            Transform target = transform.FindChild(string.Format("loadPointGroup/player{0}/headPoint/playerHead", GameController.GetInstance().intArr[index]));
            
            //播放音效
            AudioSource voice = gameObject.AddComponent<AudioSource>();
            voice.clip = Resources.Load<AudioClip>("Music/UsefulWord/" + wordID);
            voice.volume = 5;
            if (PlayerPrefs.GetFloat("AudioSet", 1f)>=0.1f)
            {
                voice.Play();
            }

            float t = voice.clip.length;
            Destroy(voice, t);

            //显示喇叭图标
            GameObject horn = new GameObject();
            horn.name = "horn";
            horn.AddComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/chat/speakIcon");
            horn.transform.SetParent(target);

            if (target.localPosition.x > 0) {
                horn.transform.localPosition = Vector3.zero + new Vector3(-25, -25, 0);
                horn.transform.localScale = new Vector3(-1.5f, 1.5f, 1.5f);
            } else {
                horn.transform.localPosition = Vector3.zero + new Vector3(25, 25, 0);
                horn.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
            }

            horn.GetComponent<Image>().color = new Color(1, 1, 1, 0);
            horn.GetComponent<Image>().DOFade(1, 0.5f).SetLoops(-1,LoopType.Yoyo);
            Destroy(horn, t);
        }
    }
